# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
# Examples:
#
#   cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
#   Mayor.create(name: 'Emanuel', city: cities.first)

admin = User.find_or_create_by_email(
  name: "Admin", email: "admin@library.com", 
  password: "thisislibrary", 
  password_confirmation: "thisislibrary",
)

admin.admin = true
admin.save

users = []

(1..5).each do |index|
  users << User.find_or_create_by_email(
    name: "User #{index}",
    email: "user_#{index}@library.com",
    password: "123456",
    password_confirmation: "123456"
  )
end

categories = []

categories << Category.find_or_create_by_name(
  name: "Novel",
  description: "This is novel"
)

categories << Category.find_or_create_by_name(
  name: "Science",
  description: "This is science book"
)

categories << Category.find_or_create_by_name(
  name: "Comic",
  description: "This is comic book"
)

books = []

(1..100).each do |index|
  books << Book.find_or_create_by_isbn(
    name: "book #{index}",
    isbn: "#{index}",
    description: "This is book #{index}",
    author: users[index % 5].name,
    publisher: users[index % 5].name,
    category_id: categories[index % 3],
    publish_date: (index % 7).days.ago
  )
end

transactions = []

Transaction.delete_all

(1..100).each do |index|
  transaction = Transaction.new(
    start_date: (index % 7).days.ago,
    end_date: (index % 7 - 2).days.ago,
    book: books[index % 10],
    user: users[index % 5]
  ) 
  transaction.status = index % 5 + 1
  transaction.save!
  transactions << transaction
end
